package com.kzy.demoadmin.web.controller;

import com.github.pagehelper.PageInfo;
import com.kzy.demoadmin.web.entity.*;
import com.kzy.demoadmin.web.service.AuditService;
import com.kzy.demoadmin.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 请假审批
 */
@RestController
@RequestMapping("audit")
public class AuditController {

    @Autowired
    private AuditService auditService;

    @Autowired
    private UserService userService;

    /**
     * 保存审批
     *
     * @param audit
     * @return
     */
    @PostMapping("/save")
    public Result add(@RequestBody Audit audit) {
        if (audit.getId() == null) {
            auditService.add(audit);
        } else {
            auditService.edit(audit);
        }
        return Result.success();
    }


    /**
     * 分页查询+模糊查询
     *
     * @param audit
     * @return
     */
    @GetMapping("/list")
    public Result getBookList(Audit audit) {
        // 调用服务层分页查询
        PageInfo<Audit> info = auditService.getAuditList(audit);
        for (Audit a : info.getList()){
            User u =  userService.findById(a.getUserId());
            a.setUserName(u.getRealName());
        }
        return Result.success(info);
    }

    /**
     * 根据id删除审批
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(auditService.deleteById(id));
    }

    /**
     * 根据id查询审批记录
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        //待完善
        
        return Result.success(auditService.findById(id));
    }

}
